package com.zye.controller;

import com.zye.entity.ResponseResult;
import com.zye.entity.User;
import com.zye.service.UserService;
import com.zye.service.UserServiceImpl;
import com.alibaba.fastjson2.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet("/login")
    public class LoginServlet extends HttpServlet {

        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            //设置请求编码
            req.setCharacterEncoding("UTF-8");
            //设置响应编码
            resp.setCharacterEncoding("UTF-8");
            //设置响应类型
            resp.setContentType("application/json;charset=UTF-8");
            //获得账号密码
            String username = req.getParameter("username");
            String password = req.getParameter("password");
            //调用service对象的登录方法
            UserService userService = new UserServiceImpl();
            User user = userService.login(username, password);
            //封装到响应结果中
            ResponseResult result = new ResponseResult(200,"OK",user);
            //发送对象给前端
            resp.getWriter().write(JSON.toJSONString(result));
        }
    }


